import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {FetchService} from '../../../core/net/fetch.service';
import {NzMessageService} from 'ng-zorro-antd';
import {Router} from "@angular/router";
import {format} from "date-fns";

@Component({
  selector: 'app-big-events',
  templateUrl: './big-events.component.html',
  styleUrls: ['./big-events.component.scss']
})
export class BigEventsComponent implements OnInit {
  form: FormGroup;
  searchForm: FormGroup;
  type = 1;
  modalTitle: string = '调度大事记';
  modalShow: boolean = false;
  nzBodyStyle: any = {
    padding: '16px 20px 0'
  };
  selectYear = '2019';
  listOfData = [];
  timeLineData = [];
  levelMap = {
    '1': '高',
    '2': '中',
    '3': '低',
  };

  recordModalShow = false;
  recordTableData = [];
  recordTotal;
  recordPageSize = 15;
  recordPageNum = 1;

  rightContentData;
  pos1Data;
  pos2Data;
  pos3Data;
  pos4Data;
  pos5Data;

  constructor(private fb: FormBuilder,
              private fetchService: FetchService,
              private router: Router,
              private message: NzMessageService) {
    this.form = this.fb.group({
      id: [null],
      title: [null],
      time: [null],
      level: [null],
      content: [null],
      remark: [null]
    });

    this.searchForm = this.fb.group({
      year: [null],
      level: [null],
      key: [null]
    });
  }

  ngOnInit(): void {
    for (let i = 0; i < 100; i++) {
      this.listOfData.push({
        name: `Edward King ${i}`,
        age: 32,
        address: `London, Park Lane no. ${i}`
      });
    }
    this.getTimelineData();
    this.getRightContentData();
  }

  getTimelineData(): void {
    this.fetchService.post<any>('archives/wrsWsEvent/leftList', {}).subscribe(res => {
      console.log(res);
      this.timeLineData = res;
    });
    this.queryInfo();
  }

  getRightContentData(): void {
    this.fetchService.post<any>('archives/wrsWsEvent/rightList', {year: this.selectYear}).subscribe(res => {
      this.rightContentData = res.map((item, index) => {
        return {...item, ...{seq: index}};
      });
      this.getPosData(res);
    });
  }

  clearPosData() {
    this.pos1Data = null;
    this.pos2Data = null;
    this.pos3Data = null;
    this.pos4Data = null;
    this.pos5Data = null;
  }

  getPosData(data): void {
    this.clearPosData();
    const len = data.length;
    if (len == 1) {
      this.pos3Data = this.rightContentData[0];
    } else if (len == 2) {
      this.pos2Data = this.rightContentData[0];
      this.pos3Data = this.rightContentData[1];
    } else if (len == 3) {
      this.pos2Data = this.rightContentData[0];
      this.pos3Data = this.rightContentData[1];
      this.pos4Data = this.rightContentData[2];
    } else if (len == 4) {
      this.pos1Data = this.rightContentData[0];
      this.pos2Data = this.rightContentData[1];
      this.pos3Data = this.rightContentData[2];
      this.pos4Data = this.rightContentData[3];
    } else if (len >= 5) {
      this.pos1Data = this.rightContentData[0];
      this.pos2Data = this.rightContentData[1];
      this.pos3Data = this.rightContentData[2];
      this.pos4Data = this.rightContentData[3];
      this.pos5Data = this.rightContentData[4];
    }
  }

  posClick(seq, step) {
    if (seq + step > 0 && (seq + step < this.rightContentData.length - 1)) {
      this.pos1Data = this.rightContentData[seq - 2];
      this.pos2Data = this.rightContentData[seq - 1];
      this.pos3Data = this.rightContentData[seq];
      this.pos4Data = this.rightContentData[seq + 1];
      this.pos5Data = this.rightContentData[seq + 2];
    }
  }

  queryInfo(reset: boolean = false): void {
    if (reset) {
      this.recordPageNum = 1;
    }
    const param = {
      year: this.searchForm.value.year,
      level: this.searchForm.value.level,
      key: this.searchForm.value.key
    };
    this.fetchService.post<any>('archives/wrsWsEvent/list', param).subscribe(res => {
      this.recordTableData = res;
    });
  }

  selectYearChange(year): void {
    this.selectYear = year;
    this.getRightContentData();
  }

  changeToTable(type): void {
    this.type = type;
    this.queryInfo();
  }

  onAdd(): void {
    this.form.reset();
    this.modalTitle = '新增调度大记事';
    this.modalShow = true;
  }

  onEdit(item): void {
    this.modalTitle = '编辑调度大记事';
    this.modalShow = true;
    this.form.patchValue(item);
  }

  onCancel(): void {
    this.modalShow = false;
  }

  onDelete(id): void {
    this.fetchService.post('archives/wrsWsEvent/delete', {ids: [id]}).subscribe(res => {
      this.message.info('删除成功');
      this.getTimelineData();
    });
  }

  onConfirm(): void {
    if (!this.form.valid) {
      this.message.warning('信息填写有误');
      return;
    }
    const param: any = {
      title: this.form.value.title,
      time: format(this.form.value.time, 'YYYY-MM-DD'),
      level: this.form.value.level,
      content: this.form.value.content,
      remark: this.form.value.remark
    };
    if (this.form.value.id) {
      param.id = this.form.value.id;
      this.fetchService.post('archives/wrsWsEvent/edit', param).subscribe(res => {
        this.message.success('操作成功！');
        this.modalShow = false;
        this.getTimelineData();
      });
    } else {
      this.fetchService.post('archives/wrsWsEvent/add', param).subscribe(res => {
        this.message.success('操作成功！');
        this.modalShow = false;
        this.getTimelineData();
      });
    }
  }
}
